#! /usr/bin/env/python3
# _*_ coding:utf-8 _*_
'''
    练习爬虫：获取一张图片 http://www.win4000.com/meitu.html
    作者：王楠
    日期：2020/03/05
    功能：
    1.0：获取图片url存到list
'''
import requests as re
from bs4 import BeautifulSoup as bs


def get_html_soup(url):
    '''
    函数：根据网页返回soup对象
    '''
    r = re.get(url, timeout=30)
    s = bs(r.text, 'lxml')
    return s


def get_url_dict():
    '''
    获取所有页面上，每一个图片集的地址
    '''
    count = 0                             # 记录图集个数
    url_dict = dict()                     # 存放每一个图片集名称和地址
    for i in range(1, 6):                 # 共5个页面
        url = 'http://www.win4000.com/meinvtag4_'+str(i)+'.html'
        s = get_html_soup(url)
        div_list_s = s.find('div', class_='Left_bar')
        for li_list_s in div_list_s.find_all('li'):
            # print(li_list_s)
            url_values = li_list_s.find('a')['href']
            url_name = li_list_s.find('img')['title']
            # print(url_name)
            # print(url_values)
            url_dict[url_name] = url_values  # 把图集名称和地址存进字典
            # rint('***************')
            count += 1                       # 计数器加一
    print('共获取到了{}个图集地址：'.format(count))
    for name, url in url_dict.items():
        print(name+' : '+url)
    return url_dict


def get_image_url(name, url):
    '''
    获取每一个图集的图片地址
    '''
    url_list = []          # 存放一个图集的图片地址
    s = get_html_soup(url)
    max_num_of_image = int(s.find('em').text)
    print(name + '  该图集共有{}张图：'.format(max_num_of_image))
    image_url_s = s.find('img', class_='pic-large')
    print(image_url_s)
    url_list.append(image_url_s['data-original'])   # 第一张图的地址
    for i in range(2, max_num_of_image+1):
        url_n = url[:-5]+'_'+str(i)+'.html'
        s = get_html_soup(url_n)
        image_url_s = s.find('img', class_='pic-large')
        url_list.append(image_url_s['data-original'])
    return url_list


def main():
    url_dict = get_url_dict()
    images_dict = dict()
    for name, url in url_dict.items():
        images_list = get_image_url(name, url)
        images_dict[name] = images_list
    print(images_dict)
    # print(s.text)


if __name__ == '__main__':
    main()
